package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public final class GraduateProjectService {
	private GraduateProjectDao graduateProjectDao= GraduateProjectDao.getInstance();
	private static GraduateProjectService graduateProjectService = new GraduateProjectService();
	
	//Singleton
	private GraduateProjectService(){}

	public static GraduateProjectService getInstance(){
		return graduateProjectService;
	}


	public Collection<GraduateProject> findAll
			(List conditionList, Pagination pagination) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<GraduateProject> graduateProjects =
				graduateProjectDao.findAll(conditionList,pagination,connection) ;
		JdbcHelper.close(connection);
		return graduateProjects;
	}

	public GraduateProject find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		GraduateProject graduateProject = graduateProjectDao.find(id,connection);
		JdbcHelper.close(connection);
		return graduateProject;
	}


	public boolean add(GraduateProject graduateProject) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean added =false;
		try {
			//增加用户
			added = graduateProjectDao.add(graduateProject,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();
            e.printStackTrace();
		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return added ;
	}

}
